package com.station.search.common.util;

import java.math.BigDecimal;

/**
 * @author: JT
 * @date: 2021/9/1
 * @Title:
 */
public class MathUtils {

    private MathUtils(){}

    public static double keep8Decimal(double d){
        BigDecimal bd=new BigDecimal(d);
        return bd.setScale(8,BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 经纬度 度分秒识别
     * @param str: 字符串经纬度
     * @return Double[] dArr
     *              0: 度  1:分 2: 秒
     */
    public static Double[] lonlatDistinguish(String str) {
        // 22°33′3.5″
        Double[] dArr = new Double[3];
        int oneIndex = str.indexOf("°");
        dArr[0] = Double.valueOf(str.substring(0, oneIndex));
        int twoIndex = str.indexOf("′");
        dArr[1] = Double.valueOf(str.substring(oneIndex+1, twoIndex));
        int threeIndex = str.indexOf("″");
        dArr[2] = Double.valueOf(str.substring(twoIndex + 1, threeIndex));
        return dArr;
    }

    /**
     * 经纬度  => 度分秒
     * @param du
     * @return Double[]
     *              0: 度 1: 分 2: 秒
     */
    public static Double[] lonlatChangeToDFM(double du) {
        int du1 = (int) du;
        double tp = (du - du1) * 60;
        int fen = (int) tp;
        String miao = String.format("%.2f", Math.abs(((tp - fen) * 60)));
        return new Double[] {
                Double.valueOf(du1), Double.valueOf(Math.abs(fen)), Double.valueOf(miao)
        };
    }
}
